//
//  ZAIPlaceholderTextView.m
//  ZAInsurance
//
//  Created by 陈鹏飞 on 17/1/9.
//  Copyright © 2017年 ZhongAn Insurance. All rights reserved.
//

#import "ZAIPlaceholderTextView.h"

@interface ZAIPlaceholderTextView()

@property (nonatomic,weak) UILabel *placeholderLabel;

@end

@implementation ZAIPlaceholderTextView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if(self) {
        
        self.backgroundColor= [UIColor clearColor];
        UILabel *placeholderLabel = [[UILabel alloc]init];
        placeholderLabel.backgroundColor= [UIColor clearColor];
        placeholderLabel.numberOfLines = 0;
        [self addSubview:placeholderLabel];
        self.placeholderLabel = placeholderLabel;
        self.myPlaceholderColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1.0];
        self.font= [UIFont systemFontOfSize:16];
        self.scrollEnabled = false;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
    }
    
    return self;
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    CGRect frame = self.placeholderLabel.frame;
    frame.origin.y = 8;
    frame.origin.x = 6;
    frame.size.width = self.frame.size.width - frame.origin.x*2.0;
    CGSize maxSize = CGSizeMake(frame.size.width,MAXFLOAT);
    frame.size.height= [self.myPlaceholder boundingRectWithSize:maxSize options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.placeholderLabel.font} context:nil].size.height;
    
    self.placeholderLabel.frame = frame;
}


- (void)setMyPlaceholder:(NSString*)myPlaceholder{
    
    _myPlaceholder= [myPlaceholder copy];

    NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:myPlaceholder];
    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:2];
    [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [myPlaceholder length])];
    [self.placeholderLabel setAttributedText:attributedString1];
    [self.placeholderLabel sizeToFit];
    
    //重新计算子控件frame
    
    [self setNeedsLayout];
}

- (void)setMyPlaceholderColor:(UIColor*)myPlaceholderColor{
    
    _myPlaceholderColor = myPlaceholderColor;
    self.placeholderLabel.textColor = myPlaceholderColor;
}
//重写这个set方法保持font一致

- (void)setFont:(UIFont*)font{
    
    [super setFont:font];
    
    self.placeholderLabel.font = font;
    [self setNeedsLayout];
}

- (void)setText:(NSString*)text{
    
    [super setText:text];
    
    [self textDidChange];
}

- (void)setAttributedText:(NSAttributedString*)attributedText{
    
    [super setAttributedText:attributedText];
    
    [self textDidChange];
}

#pragma mark -监听文字改变

- (void)textDidChange {
    
    self.placeholderLabel.hidden = self.hasText;
    self.scrollEnabled = self.hasText;
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter]removeObserver:UITextViewTextDidChangeNotification];
}


@end
